package com.ts.api.module.match.plan.handler;

import com.gitee.apanlh.util.base.CollUtils;
import com.ts.api.common.redis.ApiMatchPhoneMaskRedisKey;
import com.ts.api.common.redis.entity.ApiMatchPhoneMaskRedisCache;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.api.service.ApiBizMultiPushService;
import com.ts.api.module.match.plan.config.PlanMatchConfig;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePhoneMaskField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import com.ts.api.module.match.plan.internal.selection.PostPlanSelector;
import com.ts.api.module.match.plan.internal.selection.context.PostPlanSelectionType;
import com.ts.api.module.match.plan.model.AbstractPlanMatch;
import com.ts.api.module.match.plan.model.PlanMatchProcess;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import com.ts.api.module.product.service.ProductApplyFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  计划匹配成功处理器
 *
 *  @author Pan
 */
@Component
public class ApiPlanMatchProcessSuccessHandler {

    @Autowired
    private PostPlanSelector postPlanSelector;
    @Autowired
    private ProductApplyFlowService productApplyFlowService;
    @Autowired
    private ApiBizMultiPushService apiBizMultiPushService;

    /**
     *  计划匹配成功处理
     *
     *  @param  planMatchProcess        计划匹配处理数据实体
     *  @return ProductPlanPO           返回单个最终匹配计划
     */
    public PlanDistributeResult handleSuccess(PlanMatchProcess planMatchProcess) {
        AbstractPlanMatch abstractPlanMatch = planMatchProcess.getAbstractPlanMatch();
        ApiUserRequest sourceApiUserRequest = planMatchProcess.getSourceApiUserRequest();
        List<PlanDistributeResult> listSucMatchResult = planMatchProcess.getListMatchResult();

        //  计划结果处理-价格优先
        PlanDistributeResult finalMatchPlan = postPlanSelector.select(listSucMatchResult, PostPlanSelectionType.CHANNEL_PRICE);

        //  插入流程成功日志
        ProductApplyFlowPO currentDistributeFlow = finalMatchPlan.getCurrentDistributeFlow();
        productApplyFlowService.insert(currentDistributeFlow, false);

        //  配置类
        PlanMatchConfig planMatchConfig = abstractPlanMatch.getPlanMatchConfig();

        //  是否允许多推
        if (planMatchConfig.getAllowMultiPush()) {
            apiBizMultiPushService.multiDataExchange(sourceApiUserRequest, finalMatchPlan, listSucMatchResult);
        }
        return finalMatchPlan;
    }

    /**
     *  手机号掩码模式-计划匹配成功处理
     *
     *  @param  planMatchProcess        计划匹配处理数据实体
     *  @return List                    返回多个最终匹配计划
     */
    public List<PlanDistributeResult> handlePhoneMaskSuccess(PlanMatchProcess planMatchProcess) {
        String reqId = planMatchProcess.getReqId();
        List<PlanDistributeResult> listSucMatchResult = planMatchProcess.getListMatchResult();

        //  批量插入流程成功日志
        List<ProductApplyFlowPO> list = CollUtils.toList(listSucMatchResult, t -> t.getCurrentDistributeFlow());
        productApplyFlowService.batchInsert(list);

        //  保存唯一流程key，进件使用获取唯一请求id
        Long loanChannelId = planMatchProcess.getAbstractPlanMatch().getLoanChannelId();
        for (PlanDistributeResult planDistributeResult : listSucMatchResult) {
            PlanDistributeField planDistributeField = planDistributeResult.getPlanDistributeField();
            ProductPO productPO = planDistributeResult.getProductPO();
            ProductPlanPO productPlanPO = planDistributeResult.getProductPlanPO();

            List<PlanDistributePhoneMaskField> listPhoneMaskField = planDistributeField.getListPhoneMaskField();
            for (PlanDistributePhoneMaskField phoneMaskField : listPhoneMaskField) {
                //  根据applyId创建key
                String applyId = phoneMaskField.getApplyId();
                String saveApplyKey = ApiMatchPhoneMaskRedisKey.getApiChannelDataExchangeReqIdKey(loanChannelId, applyId);

                //  创建缓存对象
                ApiMatchPhoneMaskRedisCache apiMatchPhoneMaskRedisCache = new ApiMatchPhoneMaskRedisCache();
                apiMatchPhoneMaskRedisCache.setDataExchangeReqId(reqId);
                apiMatchPhoneMaskRedisCache.setApplyId(applyId);
                apiMatchPhoneMaskRedisCache.setProductId(productPO.getId());
                apiMatchPhoneMaskRedisCache.setProductPlanId(productPlanPO.getId());

                //  保存本次请求id
                ApiMatchPhoneMaskRedisKey.API_CHANNEL_DATA_EXCHANGE_PHONE_MASK_APPLY_ID.setForMinutes(saveApplyKey, apiMatchPhoneMaskRedisCache, 10l);
            }
        }
        return listSucMatchResult;
    }
}
